﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using RestApp.UI.Core;
using System.Web.Mvc;
using Newtonsoft.Json.Linq;

namespace RestApp.UI.Controllers
{
    public class AccountApiController : ApiController
    {
        public HttpResponseMessage Get(string userLogin)
        {
            IBusinessFactory logic = new RestApp.UI.Core.BusinessLogic();
            var user = logic.UsersLogic.GetByLogin(userLogin);

            return Request.CreateResponse(HttpStatusCode.OK, new 
            { 
                id = user.Id,
                name = user.FirstName,
                lastName = user.LastName,
                middleName = user.MiddleName,
                dateBirth = user.DateBirth == null ? "" : user.DateBirth.Value.ToString("dd.MM.yyyy"),
                salary = user.Salary,
                startWorkDate = user.StartWorkDate.ToString("dd.MM.yyyy")
            });
        }

        public HttpResponseMessage Post(PostLogin data)
        {
            IBusinessFactory logic = new RestApp.UI.Core.BusinessLogic();
            bool verify = logic.UsersLogic.VerifyUser(data.login, data.password);
            if(verify)
            {
                return Request.CreateResponse(HttpStatusCode.OK, new { success = "true" });
            }
            return Request.CreateResponse(HttpStatusCode.OK, new  { error = "true" });
        }

        public class PostLogin
        {
            public string login { get; set; }
            public string password { get; set; }
        }
    }
}
